package com.yang.service.wximpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketCollect;
import com.yang.bean.MarketCollectExample;
import com.yang.bean.MarketGoods;
import com.yang.bean.common.CommonData;
import com.yang.bean.fxy.vo.CollectVo;
import com.yang.mapper.MarketCollectMapper;
import com.yang.mapper.MarketGoodsMapper;
import com.yang.service.wx.WXCollectService;
import com.yang.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName WXCollectServiceImpl
 * @Description:
 * @Author TrueEnd
 * @Date 2022/9/11
 * @Version
 **/
@Service
public class WXCollectServiceImpl implements WXCollectService {


    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData collectList(Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andTypeEqualTo(type.byteValue());

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        List<CollectVo> collectVoList=new ArrayList<>();

        for (MarketCollect marketCollect : marketCollects) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketCollect.getValueId());
//            String string = marketGoods.getRetailPrice().toString();
//            Integer retailPrice = Integer.parseInt(string);
            CollectVo collectVo = new CollectVo(marketGoods.getBrief(), marketCollect.getId(), marketGoods.getName(), marketGoods.getPicUrl(), marketGoods.getRetailPrice(), type, marketCollect.getValueId());
            collectVoList.add(collectVo);
        }
        PageInfo pageInfo = new PageInfo(collectVoList);

        return CommonData.data(pageInfo);
    }

    @Override
    public void collectAddordelete(Integer type, Integer valueId, Integer userId) {
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        MarketCollect marketCollect = new MarketCollect(null, userId, valueId, type.byteValue(), new Date(), null, false);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        if (marketCollects.size() == 0) {
            marketCollectMapper.insertSelective(marketCollect);
        } else {
            marketCollectMapper.deleteByExample(marketCollectExample);
        }

    }

}
